﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using PlayDeezGames.Common;
using ASCII.Common;

namespace ASCII.ClickTheYellowBox
{
    public class CTYBTitleState : CTYBStateBase
    {
        private enum Button
        {
            None,
            Instructions,
            Start,
            About
        }
        private Button hoverButton = Button.None;
        private Button pressedButton = Button.None;

        public CTYBTitleState(ASCIIRenderer theRenderer):base(theRenderer)
        {
        }

        protected override void OnStart()
        {
            Renderer.SetCells(0, 0, Renderer.GridBoard.Columns, Renderer.GridBoard.Rows, 0, ASCIICellColor.Black, ASCIICellColor.Black);
            Renderer.DrawTable(TableDrawingPalette.V2H2Palette, new int[] { 0, Renderer.GridBoard.Columns - 1 }, new int[] { 0, Renderer.GridBoard.Rows - 1 }, ASCIICellColor.Blue, ASCIICellColor.DarkGray, false);
        }

        protected override void OnIdle()
        {
        }

        protected override void OnFinish()
        {
        }

        private void OnMouseDown(MouseButtonEventArgs e)
        {
            if (hoverButton != Button.None)
            {
                pressedButton = hoverButton;
            }
            //OnDraw();
        }

        private void OnMouseUp(MouseButtonEventArgs e)
        {
            if (hoverButton == pressedButton)
            {
                switch (pressedButton)
                {
                    case Button.Instructions:
                        ExitState = CTYBState.Instructions;
                        break;
                    case Button.About:
                        ExitState = CTYBState.About;
                        break;
                    case Button.Start:
                        ExitState = CTYBState.Play;
                        break;
                    default:
                        throw new NotImplementedException();
                }
                Finish();
                Parent.OnMessage(new MessageBase(StateMachine<CTYBState>.MSGID_Step));
            }
            pressedButton = Button.None;
            //OnDraw();
        }

        private void OnMouseMove(MouseEventArgs e)
        {
            Button oldHoverButton = hoverButton;
            hoverButton = Renderer.MouseInButton(4, 25, "Instructions", null, e) ? Button.Instructions :
                Renderer.MouseInButton(20, 25, "Start", null, e) ? Button.Start :
                Renderer.MouseInButton(29, 25, "About", null, e) ? Button.About :
                Button.None;
            if (oldHoverButton != hoverButton)
            {
                //OnDraw();
            }
        }

        private void OnDraw()
        {

            Renderer.DrawButton(4, 25, "Instructions", pressedButton == Button.Instructions, hoverButton==Button.Instructions, false, true);
            Renderer.DrawButton(20, 25, "Start", pressedButton == Button.Start, hoverButton == Button.Start, true, true);
            Renderer.DrawButton(29, 25, "About", pressedButton == Button.About, hoverButton == Button.About, false, true);

            Renderer.Render();
        }

        public override bool OnMessage(MessageBase theMessage)
        {
            if (theMessage.MessageId == CTYBStateMachine.MSGID_Draw)
            {
                OnDraw();
                return true;
            }
            else if (theMessage.MessageId == CTYBStateMachine.MSGID_MouseMove)
            {
                OnMouseMove((theMessage as MessageWithPayload<MouseEventArgs>).Payload);
                return true;
            }
            else if (theMessage.MessageId == CTYBStateMachine.MSGID_MouseDown)
            {
                OnMouseDown((theMessage as MessageWithPayload<MouseButtonEventArgs>).Payload);
                return true;
            }
            else if (theMessage.MessageId == CTYBStateMachine.MSGID_MouseUp)
            {
                OnMouseUp((theMessage as MessageWithPayload<MouseButtonEventArgs>).Payload);
                return true;
            }
            else
            {
                return base.OnMessage(theMessage);
            }
        }
    }
}
